const models = require("../database/models.js");
let Chat = models.getModel("chat");
module.exports = function socket(io) {
  io.sockets.on("connection", function (socket) {
    // 发送信息
    socket.on("sendmsg", (data) => {
      const { from, to, msg } = data;
      const chatid = [from, to].sort().join("_");
      if (to) {
        let chatModel = new Chat({ chatid, from, to, content: msg });
        chatModel.save((err, doc) => {;
          io.emit("receivemsg", Object.assign({}, doc));
        });
      }
    });
  });
};
